package com.leyou.auth.Servcie.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.Servcie.SecretService;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.SecretMapper;
import com.leyou.common.exceptions.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class SecretServiceImpl extends ServiceImpl<SecretMapper, ClientInfo> implements SecretService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${ly.jwt.key}")
    private String key;
    /**
     * 验证服务信息是否正确
     * @param clientId
     * @param secret
     * @return
     */
    @Override
    public String getSecretKey(String clientId, String secret) {
        //因为密码是加密的，所以先创建加密算法的对象。BcryptPasswordEncode
        //根据clientId查询
        ClientInfo clientInfo = query().eq("client_id", clientId).one();
        //判断是否存在
        if(clientInfo==null){
            throw new LyException("服务端信息不正确",400);
        }
        //进行密码匹配
        boolean matches = passwordEncoder.matches(secret, clientInfo.getSecret());

        //正确，返回秘钥，
        if(!matches){
            throw new LyException("身份信息有误",400);
        }
        return key;
    }
}
